package cn.iocoder.yudao.module.vote.controller.admin.voteconfig;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.vote.controller.admin.voteconfig.vo.*;
import cn.iocoder.yudao.module.vote.dal.dataobject.voteconfig.VoteConfigDO;
import cn.iocoder.yudao.module.vote.convert.voteconfig.VoteConfigConvert;
import cn.iocoder.yudao.module.vote.service.voteconfig.VoteConfigService;

@Api(tags = "管理后台 - 优秀最大比例配置信息")
@RestController
@RequestMapping("/vote/config")
@Validated
public class VoteConfigController {

    @Resource
    private VoteConfigService configService;

    @PostMapping("/create")
    @ApiOperation("创建优秀最大比例配置信息")
    @PreAuthorize("@ss.hasPermission('vote:config:create')")
    public CommonResult<Long> createConfig(@Valid @RequestBody VoteConfigCreateReqVO createReqVO) {
        return success(configService.createConfig(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新优秀最大比例配置信息")
    @PreAuthorize("@ss.hasPermission('vote:config:update')")
    public CommonResult<Boolean> updateConfig(@Valid @RequestBody VoteConfigUpdateReqVO updateReqVO) {
        configService.updateConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除优秀最大比例配置信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('vote:config:delete')")
    public CommonResult<Boolean> deleteConfig(@RequestParam("id") Long id) {
        configService.deleteConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得优秀最大比例配置信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('vote:config:query')")
    public CommonResult<VoteConfigRespVO> getConfig(@RequestParam("id") Long id) {
        VoteConfigDO config = configService.getConfig(id);
        return success(VoteConfigConvert.INSTANCE.convert(config));
    }

    @GetMapping("/getByVoteId")
    @ApiOperation("获得优秀最大比例配置信息")
    @ApiImplicitParam(name = "voteId", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('vote:config:query')")
    public CommonResult<VoteConfigRespVO> getConfigByVoteId(@RequestParam("id") Integer voteId) {
        VoteConfigDO config = configService.selectByVoteId(voteId);
        return success(VoteConfigConvert.INSTANCE.convert(config));
    }

    @GetMapping("/list")
    @ApiOperation("获得优秀最大比例配置信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('vote:config:query')")
    public CommonResult<List<VoteConfigRespVO>> getConfigList(@RequestParam("ids") Collection<Long> ids) {
        List<VoteConfigDO> list = configService.getConfigList(ids);
        for(VoteConfigDO voteConfigDO:list){
            if(voteConfigDO.getId()==1){
                list.remove(voteConfigDO);
                break;
            }
        }
        return success(VoteConfigConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得优秀最大比例配置信息分页")
    @PreAuthorize("@ss.hasPermission('vote:config:query')")
    public CommonResult<PageResult<VoteConfigRespVO>> getConfigPage(@Valid VoteConfigPageReqVO pageVO) {
        PageResult<VoteConfigDO> pageResult = configService.getConfigPage(pageVO);
        for(VoteConfigDO voteConfigDO:pageResult.getList()){
            if(voteConfigDO.getId()==1){
                pageResult.getList().remove(voteConfigDO);
                break;
            }
        }
        return success(VoteConfigConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出优秀最大比例配置信息 Excel")
    @PreAuthorize("@ss.hasPermission('vote:config:export')")
    @OperateLog(type = EXPORT)
    public void exportConfigExcel(@Valid VoteConfigExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<VoteConfigDO> list = configService.getConfigList(exportReqVO);
        // 导出 Excel
        List<VoteConfigExcelVO> datas = VoteConfigConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "优秀最大比例配置信息.xls", "数据", VoteConfigExcelVO.class, datas);
    }

}
